package com.jierui.meter.frontend.web.rest.front;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.jierui.meter.frontend.base.util.JacksonUtil;
import com.jierui.meter.frontend.domain.DO.CityOrgDO;
import com.jierui.meter.frontend.domain.DO.OrgDO;
import com.jierui.meter.frontend.domain.DO.ShowIndicesDO;
import com.jierui.meter.frontend.domain.VO.CityAndOrgOptionVO;
import com.jierui.meter.frontend.service.CityOrgService;
import com.jierui.meter.frontend.service.OrgService;
import com.jierui.meter.frontend.service.ShowIndicesService;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("dashboard")
public class DashbordResource {

	@Autowired
	private CityOrgService cityOrgService;
	
	@Autowired
	private OrgService orgService;
	
	@Autowired
	private ShowIndicesService showIndicesService;
	
	private static final Log log = LogFactory.getLog(DashbordResource.class);
	/**
	 * 获取地市和县数据
	 * @return
		返回的json格式：
		{	
			status : "200",  //接口状态，类型为字符串，“200为请求成功
			data : {
				scope:[ //市级范围
					{
						“name”: ”西安”,  // name用于页面显示
						“value”: ”12312”  // value用于接口传值
					},
					…
					],
				org:[ //县级范围
					{
						“name”: ”西安XX供电公司”, // name用于页面显示
						“value”: ”1231301” // value用于接口传值
						“pvalue”: ”12313” // 对应scope的value，用于和scope联动
					},
					…
					]
			} //data数据结束
		}
	 * @throws Exception 
	*/
	@ResponseBody
	@RequestMapping(value = "/option", method = RequestMethod.GET)
	public Map<String, Object> getCityAndOrgOptionData() throws Exception {

		Map<String, Object> resMap = new HashMap<>();
		Map<String, Object> data = new HashMap<>();

		// TODO add try catch
		List <CityOrgDO>  cityOrgList = cityOrgService.select();
		List <OrgDO>  orgList = orgService.select();

		if ( cityOrgList == null ){
			cityOrgList = new ArrayList <CityOrgDO>();
		}
		CityOrgDO defaultCityOrgDO = new CityOrgDO();
		defaultCityOrgDO.setOrgName("全部");
		defaultCityOrgDO.setOrgNo("_");
		
		cityOrgList.add(0, defaultCityOrgDO);
		
		if ( orgList == null ){
			orgList = new ArrayList <OrgDO>();
		}
		OrgDO defaultOrgDO = new OrgDO();
		defaultOrgDO.setOrgName("全部");
		defaultOrgDO.setOrgNo("_");
		defaultOrgDO.setPOrgNo("_");
		orgList.add(0, defaultOrgDO);
		
		data.put("scope",cityOrgList);
		data.put("org", orgList);

		resMap.put("status", "200");
		resMap.put("data", data);

		return resMap;
	}
	
	
	@ResponseBody
	@GetMapping("")
	public Map<String, Object> getDashbordData( HttpServletRequest request) throws Exception {

		Map<String, Object> resMap = new HashMap<>();
		Map<String, Object> data = new HashMap<>();
		int tgNum =0;
		int meterNum =0;
		int invalidMeterNum =0;
		
		// TODO add try catch
		
		String year = request.getHeader("date");
		String scope = request.getHeader("scope");
		String tg = request.getHeader("org");
		
		data.put("tg_num", 0);
		data.put("meter_num", 0);
		data.put("invalid_meter_num", 0);	
		data.put("alarm_event_num", 0);
		
		
		ShowIndicesDO siObj = showIndicesService.selectIndices(year, scope, tg);
		
		if( siObj != null ){
			data.put("tg_num", siObj.getNTg());
			data.put("meter_num", siObj.getNMeter());
			data.put("invalid_meter_num", siObj.getNErrMeter());	
			data.put("alarm_event_num",  siObj.getNErrMeter() );
		}

		resMap.put("status", "200");
		resMap.put("data", data);

		return resMap;
	}

}
